// packageStore/pages/distribution/commission/index.js
import BasePage from '../../../js/common/base-page';
import {api} from '../../../js/util';
import {distribution} from '../../../js/modules/distribution.js';
import {wholesaleSettings} from "../../../js/modules/gloab";
BasePage({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    tablist: ['待结算', '已结算'],
    activeIndex: 0,
    status:1,//1:待结算，2：已结算
    hasMoreData: true,
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 0,
    commissionList:[],
    PageIndex: 1,
    PageSize: 20,
    priceDigits:2,
    userId:0,
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.userid){
      this.setData({
        userId:options.userid
      })
    }
    let that = this;
    wx.getSystemInfo({
      success: (res)=> {
        that.setData({
          slidewidth: (res.windowWidth / that.data.tablist.length) - 20,
        });
      }
    });
    // this.getWholesaleConfig();
    this.loadList();
  },
  /**
   * 获取页面配置
   */
  getWholesaleConfig() {
    var that = this;
    var config = wholesaleSettings.getConfig();
    that.setData({
      priceDigits: config.priceDigits,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadList()
    this.setData({
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.PageIndex = 1;
    this.loadList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    if (this.data.hasMoreData) { 
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 加载列表
   */
  loadList:function(){
    let that = this;
    let {PageIndex,PageSize,userId,status} = this.data;
    let args = {
      status:status,
      pageIndex:PageIndex,
      pageSize:PageSize,
    };
    distribution.loadCommisionList(userId,args,function(content){
      var list = that.data.commissionList;
      if (that.data.PageIndex == 1) {
        list = [];
      }
      if (content.count > 0) {
        var contentlist = content.data;
        if (contentlist.length < that.data.PageSize) {
          that.setData({
            commissionList: list.concat(contentlist),
            hasMoreData: false
          });
        } else {
          that.setData({
            commissionList: list.concat(contentlist),
            hasMoreData: true,
            PageIndex: that.data.PageIndex + 1
          });
        }
  
      } else {
        that.setData({
          commissionList: [],
          hasMoreData: true,
          PageIndex: 1
        });
      }
    })
  },
   /**
   * 切换当前选择项
   */
  changeCurrent: function (res) {
    if (this.data.activeIndex == res.detail.currentNum.id) return;
    var activeIndex = res.detail.currentNum.id;
    var status=activeIndex==0?1:2;
    var that=this
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: activeIndex,
      status:status,
      PageIndex:1
    },()=>{
      that.loadList()
    });
  }
})